package com.thssh.ui.lsn10.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import java.util.Arrays;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/25
 * email: zhangyugehu@163.com
 */

public class PathMeasureView extends View {
    private Paint mPaint;
    private Paint mTextPaint;
    private Path mPath;

    private float animValue;

    public PathMeasureView(Context context) {
        this(context, null);
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);
        mTextPaint.setTextSize(40);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setStyle(Paint.Style.FILL);
        mPath = new Path();

        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(5000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        getLength(canvas);
//        getSegment(canvas);
//        newtCounter(canvas);
        getMatrix(canvas);
    }

    private void getPosTan(Canvas canvas) {
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);
        mPath.reset();
        mPath.moveTo(100,100);
        mPath.quadTo(250, 300, 500, 100);
        mPath.rQuadTo(100, -100, 400, 0);
        PathMeasure measure = new PathMeasure(mPath, false);
        float[] pos = new float[2];
        float[] tan = new float[2];
        measure.getPosTan(animValue * measure.getLength(), pos, tan);
        canvas.drawPath(mPath, mPaint);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(5);
        canvas.drawPoint(pos[0], pos[1], mPaint);
        float degree = (float) (Math.atan2(tan[1], tan[0])*180f / Math.PI);
        canvas.save();
        canvas.translate(pos[0], pos[1]);
        canvas.rotate(degree);
        canvas.drawLine(-200,0,200,0, mPaint);
        canvas.restore();
    }

    private void getMatrix(Canvas canvas) {
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);
        mPath.reset();
        mPath.moveTo(100,100);
        mPath.quadTo(250, 300, 500, 100);
        mPath.rQuadTo(100, -100, 400, 0);
        PathMeasure measure = new PathMeasure(mPath, false);
        Matrix matrix = new Matrix();
        measure.getMatrix(animValue * measure.getLength(), matrix, PathMeasure.POSITION_MATRIX_FLAG|PathMeasure.TANGENT_MATRIX_FLAG);
        canvas.drawPath(mPath, mPaint);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(5);
        canvas.save();
        canvas.setMatrix(matrix);
        // 画箭头
        canvas.save();
        canvas.rotate(-20);
        canvas.drawLine(-50,0, 0,0, mPaint);
        canvas.restore();
        canvas.save();
        canvas.rotate(20);
        canvas.drawLine(-50,0, 0,0, mPaint);
        canvas.restore();
        // 箭头结束
        canvas.restore();
    }

    private void newtCounter(Canvas canvas) {
        mPath.reset();
        mPath.addRect(100, 100, 300, 300, Path.Direction.CCW);
        mPath.addCircle(200, 200, 100, Path.Direction.CCW);
        PathMeasure pathMeasure = new PathMeasure(mPath, false);
        StringBuilder sb = new StringBuilder();
        while(pathMeasure.nextContour()){
            sb.append(pathMeasure.getLength() + " >>> ");
        }
        drawText(sb.toString(), canvas);
    }

    private void getSegment(Canvas canvas) {
        mPath.reset();
        mPath.moveTo(100, 100);
        mPath.addRect(200, 200, 400, 400, Path.Direction.CCW);
        PathMeasure pathMeasure = new PathMeasure(mPath, false);
        Path dstPath = new Path();
        dstPath.lineTo(150, 150);
        float length = pathMeasure.getLength();
        pathMeasure.getSegment(0, length/2, dstPath, false);
        mPaint.setColor(Color.YELLOW);
        canvas.drawPath(mPath, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawPath(dstPath, mPaint);
        drawText("getSegment[0, " + length /2 + "]", canvas);
    }

    private void getLength(Canvas canvas) {
        mPath.reset();
        mPath.moveTo(100, 100);
        mPath.lineTo(100,200);
        mPath.lineTo(200, 200);
        mPath.lineTo(200, 100);

        PathMeasure pathMeasure1 = new PathMeasure(mPath, false);
        PathMeasure pathMeasure2 = new PathMeasure(mPath, true);
        canvas.drawPath(mPath, mPaint);
        drawText("getLength: " + pathMeasure1.getLength() + " || getLength forceClosed: " + pathMeasure2.getLength(), canvas);
    }

    private void drawText(String text, Canvas canvas) {
        canvas.drawText(text, 10, 50, mTextPaint);
    }
}
